//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.cn.common.utils.tree;

import java.util.*;

public class TreeUtil {
    public TreeUtil() {
    }

    public static <T extends TreeNode<T>> List<T> toTree(Collection<T> list) {
        List<T> result = new LinkedList();
        Map<Long, T> map = new LinkedHashMap();

        TreeNode t;
        Long id;
        for(Iterator var3 = list.iterator(); var3.hasNext(); map.put(id, (T) t)) {
            t = (TreeNode)var3.next();
            id = t.getId();
            Long parentId = t.getParentId();
            Integer depth = t.getDepth();
            if (depth != null && depth != 0 && parentId != null && map.containsKey(parentId)) {
                T parent = (T) map.get(parentId);
                List<T> children = parent.getChildren();
                if (null == children) {
                    children = new ArrayList();
                }
                ((List)children).add(t);
                parent.setChildren((List)children);
            } else {
                result.add((T) t);
            }
        }

        return result;
    }
}
